#include "socket.hpp"
#include "protocol.hpp"
#define NUM 1024

void Usage(const string &proc)
{
    cout << "you can use " << proc << "ip port " << endl;
}
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(1);
    }
    int sock = Sock::Socket();
    Sock::Connect(sock, atoi(argv[2]), argv[1]);
    // 业务逻辑
    request_t rq;
    memset(&rq, 0, sizeof(rq));
    cout << "请输入第一个操作数: ";
    cin >> rq.x;
    cout << "请输入第二个操作数：";
    cin >> rq.y;
    cout << "请输入你想要执行的操作的操作符: ";
    cin >> rq.op;

    //序列化
    string buffer = serialize_request(rq);
    cout << buffer << endl; 
    write(sock, buffer.c_str(), buffer.length());

    //构建响应
    char ret_response[NUM] = {0};
    ssize_t ret = read(sock, ret_response, sizeof(ret_response));
    if (ret > 0)
    {
        //反序列化
        response_t resp;
        memset(&resp, 0, sizeof(resp));
        string str = ret_response;
        deserialize_response(str, resp);

        cout << "code : " << resp.code << endl;
        cout << "result: " << resp.result << endl;
    }

    return 0;
}